package com.pda.common.pages.createproductbill;

import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.pda.common.R;
import com.pda.common.basemvp.activity.BaseActivity;
import com.pda.common.bean.BillBean;
import com.pda.common.database.ProductInfo;
import com.pda.common.dialogs.product.ProductDialog;
import com.pda.common.pages.relationscanf.RelationScanfCActivity;
import com.pda.common.utils.PDAUtil;
import java.io.Serializable;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class CreateProductBillActivity extends BaseActivity<CreateProductBillModel, CreateProductBillView, CreateProductBillPresent> implements CreateProductBillView {
    private String TAG = "CreateProductBillActivity";
    @BindView(R.id.iv_title_left)
    ImageView ivTitleLeft;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tvright)
    TextView tvright;
    @BindView(R.id.edpnam)
    TextView edpnam;
    @BindView(R.id.edbatch)
    EditText edbatch;
    @BindView(R.id.loginbt)
    Button loginbt;
    ProductDialog productDialog = null;
    ProductInfo pi=null;
    String batch="";

    @Override
    protected void initdata() {
        tvTitle.setText("新建生产订单");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_createproductbill);
        ButterKnife.bind(this);
        initdata();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }


    @OnClick({R.id.iv_title_left, R.id.loginbt,R.id.edpnam})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_title_left:
                finish();
                break;
            case R.id.edpnam:
                if(productDialog==null)
                {
                    productDialog = new ProductDialog(mContext, R.style.CommentDialog, new ProductDialog.PriorityListener() {
                        @Override
                        public void setActivityText(ProductInfo productInfo) {
                            pi=productInfo;
                            edpnam.setText(pi.getName());
                        }
                    });
                    WindowManager.LayoutParams lp = productDialog.getWindow().getAttributes();
                    lp.gravity = Gravity.BOTTOM;
                    lp.width = LinearLayout.LayoutParams.MATCH_PARENT;
                }
                productDialog.show();
                break;
            case R.id.loginbt:
                if(pi==null)
                {
                    showMsg("请选择产品");
                    return;
                }
                batch = edbatch.getText().toString();
                if(batch.length()<=0)
                {
                    showMsg("请填写批次");
                    return;
                }
                presenter.createBill(pi.getPid(),batch);
                break;
        }
    }

    @Override
    public CreateProductBillModel createModel() {
        return new CreateProductBillModelImpl(this);
    }

    @Override
    public CreateProductBillView createView() {
        return this;
    }

    @Override
    public CreateProductBillPresent createPresenter() {
        return new CreateProductBillPresent();
    }

    @Override
    public void CreateBillSucc(final BillBean billBean) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                intentToActivityWithSParameter(CreateProductBillActivity.this, RelationScanfCActivity.class, PDAUtil.keyBillDeatial, (Serializable)billBean);
                CreateProductBillActivity.this.finish();
            }
        });
    }

    @Override
    public void tip(final String str) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                showMsg(str);
            }
        });
    }
}
